#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QFileDialog>
#include <QMouseEvent>
#include <QMessageBox>

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

#include "opencvtoqt.h"

#include <QDebug>

using namespace std;
using namespace cv;

//命名规范
//类名首字母大写，单词和单词之间首字母大学
//函数名、变量名的首字母小写，单词和单词之间的首字母大写

//快捷键
//运行 ctrl + r
//编译 ctrl + b
//注释 ctrl + /
//字体缩放 ctrl + 鼠标混轮
//整行移动 ctrl + shift + 上下键
//自动对齐 ctrl + i
//同名的h和cpp之间切换 F4
//查找关键字 ctrl + f
//帮助文档 f1

#define DEFAULT_FRM_NUMS (600)

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


//    QPushButton *pbtn = new QPushButton();
//    //pbtn->show();//以顶层的方式显示
//    //如果想显示的时候依赖在当前的窗口中，需要设置父窗口
//    pbtn->setParent(this);
//    //设置文本
//    pbtn->setText("您好");
//    QPushButton *pbtn2 = new QPushButton("您好",this);
//    pbtn2->move(200,100);
//    //设置窗口尺寸
//    this->resize(800,600);
//    // 设置固定尺寸窗口
//    this->setFixedSize(600,400);


    //初始化
    init();
}

Widget::~Widget()
{
    delete ui;
}

int Widget::getTabWidgetNums()
{
    return ui->tabWidget->count();
}

int Widget::getFrameNums()
{
    return this->frmNums;
}

void Widget::setFrameNums(int nums)
{
    this->frmNums = nums;
}

QString Widget::getFilePath()
{
    return this->filePath;
}

void Widget::setFilePath(QString filePath)
{
    this->filePath = filePath;
}

void Widget::mousePressEvent(QMouseEvent *e)//实例一个鼠标事件的对象e
{
    //单击鼠标事件函数，当点击鼠标时，触发该函数执行
   qDebug() <<"e->x: " << e->x() <<" e_.y: " << e->y() <<endl;//软件界面
   qDebug() <<"globalX: " << e->globalX() << "globalY: " << e->globalY() <<endl; //屏幕
   qDebug() <<"pos: " << e->pos() <<endl; //软件界面坐标点


}

void Widget::init()
{
    //tab总数
    const int TABNUMS = getTabWidgetNums();

    //点击按钮，选择文件，弹出文件对话框，读取路径内的文件
    connect(ui->pushButton,&QPushButton::clicked,[=](){
       QString fileName = QFileDialog::getOpenFileName(this,
            tr("Open Image"), "F:\\Proj\\IRTemp\\code\\aVideoAnalysis", tr("Image Files (*.*)"));
       setFilePath(fileName);
       ui->filePathLineEdit->setText(getFilePath());
       qDebug() << "fileName: " <<fileName <<endl;
        setFrameNums(500);
        //创建显示容器
        QGraphicsScene *scene = new QGraphicsScene;
        //向容器中添加文件路径为fileName（QString类型）的文件
        QPixmap* pixMap = new QPixmap(fileName);
        if((pixMap->width() != 640) || (pixMap->height() != 512))
        {
            QMessageBox::information(NULL, "提示信息", "输入图片错误");
        }
        qDebug() << "width: " <<pixMap->width() << "height: " << pixMap->height() <<endl;

        QImage qImg  = pixMap->toImage();
        Mat img =  OpenCVtoQt::QImageToCvMat(qImg);
        Mat img2,img3;
        //图像处理
        cvtColor(img,img2,COLOR_BGR2GRAY);
        //threshold(img2,img2,50,200,THRESH_BINARY);
        applyColorMap(img2,img3,cv::COLORMAP_JET);
        QPixmap temp = OpenCVtoQt::cvMatToQPixmap(img3);
        //ui->label_10->setPixmap(temp);

        scene->addPixmap(*pixMap);
        scene->addLine(QLine(0,0,200,134),QPen(Qt::green));

        //关闭拖条bar
        ui->IRGraphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        ui->IRGraphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        //关闭拖拽
        ui->IRGraphicsView->setDragMode(QGraphicsView::NoDrag);
        ui->IRGraphicsView->setCursor(Qt::CrossCursor);


        //借助graphicsView（QGraphicsView类）控件显示容器的内容
        ui->IRGraphicsView->setScene(scene);
        //开始显示
        ui->IRGraphicsView->show();

        //cv::Mat inputImage = cv::imread(getFilePath().toLocal8Bit().toStdString());


    });

    //设置默认帧数
    setFrameNums(DEFAULT_FRM_NUMS);
   //工具按钮槽关联
    connect(ui->pointBtn,&QPushButton::clicked,[=](){
        disableUnuseTabs(0,TABNUMS);
    });
    connect(ui->lineBtn,&QPushButton::clicked,[=](){
        disableUnuseTabs(1,TABNUMS);
    });
    connect(ui->ellipseBtn,&QPushButton::clicked,[=](){
        disableUnuseTabs(2,TABNUMS);
    });

    //Slider Spinbox设置
    ui->playSlider->setMinimum(0);
    ui->playSlider->setMaximum(getFrameNums());
    ui->playSlider->setSingleStep(1);

    ui->spinBox->setMinimum(0);
    ui->spinBox->setMaximum(getFrameNums());

    //qt4的使用方法
    //connect(ui->playSlider,SIGNAL(valueChanged(int)), ui->spinBox, SLOT(setValue(int)));
    //connect(ui->spinBox,SIGNAL(valueChanged(int)), ui->playSlider, SLOT(setValue(int)));
    //qt5的使用方法
    void (QSpinBox::* ptrQSpinBox)(int) = &QSpinBox::valueChanged; //在外部，定义一个信号的函数指针，用这个指针来申明这个函数指向的地址
    connect(ui->playSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
    connect(ui->spinBox,ptrQSpinBox,ui->playSlider,&QSlider::setValue);


    //在此处更新视频播放
    connect(ui->playSlider,&QSlider::valueChanged,[=](){
        qDebug() << "fileName: " <<ui->playSlider->value() <<endl;
    });

    qDebug() << "ui->IRView->x(): " << ui->IRGraphicsView->x() << "ui->IRGraphicsView->y(): " << ui->IRGraphicsView->x() << endl;

}
void Widget::disableUnuseTabs(int index, int tabNums)
{
    //使能indextab,去使能其余的tab
    //ui->tabWidget->setTabEnabled(1, false);
    for (int i = 0; i < tabNums ; i++) {
        if(i != index)
        {
            ui->tabWidget->setTabEnabled(i, false);
        }
        else
        {
            ui->tabWidget->setTabEnabled(i, true);
        }
    }
}
